package Ctr;

import DB.ConnectionDB;
import DB.GetMax;
import DB.SupplierDB;
import Model.Supplier;
import java.util.ArrayList;

public class SupplierCtr {

    private ConnectionDB connection;
    private SupplierDB supplierDB;

    public SupplierCtr() {
        supplierDB = new SupplierDB();
    }

    //Insert supplier
    public void insertSupplier(int id, String name, String address, String country, int phoneNo, String email) {
        Supplier supObj = new Supplier(id, name, address, country, phoneNo, email);

        try {
            connection.startTransaction();
            SupplierDB supplierDB = new SupplierDB();
            supplierDB.insertSupplier(supObj);
            connection.commitTransaction();
        }
        catch (Exception e) {
            connection.rollbackTransaction();
        }
    }

    //Find supplier
    public Supplier findSupplier (int id) {
        return supplierDB.findSupplier(id);
    }

    //Find all suppliers
    public ArrayList<Supplier> getAllSuppliers(boolean retrieveAssociation) {
        return supplierDB.getAllSuppliers(false);
    }

    //Update supplier
    public void updateSupplier(int id) {
        supplierDB.updateSupplier(supplierDB.findSupplier(id));
    }

    //Delete supplier
    public void deleteSupplier(int id) {
        supplierDB.deleteSupplier(id);
    }

    //Get max
    public int getMaxId() {
        int id = supplierDB.supplierGetMax();
        return id;
    }
}
